/* LEDC (LED Controller) fade example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "freertos/queue.h"

#include "esp_err.h"
#include "esp_mac.h"
#include "esp_log.h"

#include "esp_system.h"

#include "my_system_param.h"
#include "my_seg_screen_drv.h"

static const char *TAG = "my_sys";
//队列句柄
QueueHandle_t my_main_queue;



/*************************************************************
 * my_main_handler_task
 * 主的接消息任务
**************************************************************/
static void my_main_handler_task(void *arg)
{
    uint8_t duty = 0;
    (void) arg; //Unused
    MAIN_MSG_t recv_msg;

    disp_data_t disp_data = {0};

    while (1) {
        /* Waiting for touch element messages */
        if(xQueueReceive(my_main_queue, &(recv_msg), portMAX_DELAY))
		{ 
            switch(recv_msg.type){
                case KEY_TYPE:{
                    ESP_LOGI(TAG, "key_msg:[%d]-[%d] \r\n", recv_msg.data.key.id, recv_msg.data.key.event);
                    disp_data.display_data ++;
                    disp_data.flick_type = GE_FLICK;

                    disp_data.flick_time %= 8; 
                    disp_data.flick_time ++;
                    ESP_LOGI(TAG, "send:[%d] \r\n", disp_data.flick_time);
                    xQueueSend(g_display_queue, (void*)&disp_data, (TickType_t)0);

                    
                    break;
                }
                case ROTATION_TYPE:{
                    ESP_LOGI(TAG, "rotation_msg:[%d]-[%d] \r\n", recv_msg.data.rota.id, recv_msg.data.rota.event);
                    break;
                }
                case NTC_TYPE:
                {
                    ESP_LOGI(TAG, " NTC_msg \r\n");
                    break;
                }
                case BLE_TYPE:{
                    ESP_LOGI(TAG, " BLE_TYPE \r\n");
                    break;
                }
                case WIFI_TYPE:{
                    ESP_LOGI(TAG, " WIFI_TYPE \r\n");
                    break;
                }
                default:{
                    break;
                }
            }
		}
    }
}



/*************************************
*
*
**************************************/
void my_system_init(void)
{
    int ret = 0;

    my_main_queue = xQueueCreate(10, sizeof(MAIN_MSG_t));
    //创建队列返回已创建队列的句柄
	if(my_main_queue == NULL){ 
        ESP_LOGE(TAG, "failed to create queue1= %p \n", my_main_queue);
        return;
	}
    ret = xTaskCreate(&my_main_handler_task, "fans_handler_task", 2 * 1024, NULL, 5, NULL);
    
    if(ret == 0){
        ESP_LOGE(TAG, "creat my_main_handler_task err!!");
		return;
    }
	ESP_LOGI(TAG, "my_main_handler_task succ");

}




